// pages/check/check.js
const api = require('../../utils/request.js')
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: "",
    hospitalId:"",
    options: [
      {
        text: '全部',
        type: 'radio',
        options: []
      }
    ],
    title: {
      noName: "发票号码",
      codeName: "发票代码",
      moneyName: "不含税金额",
      no: "", //发票号码
      code: "", //发票代码
      money: "" //金额
    },
    realInvoice: {
      no: "", //发票号码
      code: "", //发票代码
      money: "" //金额
    },
    sysInvoice: {
      no: "", //发票号码
      code: "", //发票代码
      money: "" //金额
    },
    placeholder: "请输入票据单号",
    confirmType: "search",
    userinfo: {
      avatarUrl: "/static/img/unlogin.png",
      nickName: "点击登录"
    },
    btnState: true
  },
  confirm(event) {
    console.log("点击了搜素按钮！" + event.detail);
  },
  onClickLeft(e){
    wx.navigateTo({
      url: '/pages/login/login'
    })
  },
  //监听输入框是否有值
  onInvoiceNo(e) {
    let invoice = this.data.realInvoice;
    let value = e.detail.value;
    invoice.no = value;
    // this.setData({
    //     realInvoice.no:value
    // }) 
    value.length == 0 ? (this.setData.btnState = true) : (this.setData.btnState = false);
    console.log("btnState:" + this.data.btnState);
  },
  onInvoiceCode(e) {
    let invoice = this.data.realInvoice;
    let value = e.detail.value;
    invoice.code = value;
    // this.setData({
    //   realInvoice: {
    //     code: value
    //   }
    // })
    value.length == 0 ? (this.setData.btnState = true) : (this.setData.btnState = false);
    console.log("btnState:" + this.data.btnState);
  },
  onDelete(event) {
    let itemIndex = event.currentTarget.dataset.itemIndex;
    console.log("icon : " + event.currentTarget.dataset.itemIndex);
    this.invoiceNos.splice(itemIndex, 1);
  },
  onInput(e) {
    console.log("input: ", e.detail.value);
  },
  onConfirm(e) {
    console.log("confirm: ", e);
  },
  onSearch(e) {
    console.log("onSearch: ", e);
  },
  submitForm(e) {
    console.log("submit", e);
  },
  //点击医院事件
  handleHospital(e) {
    console.log(JSON.stringify(this.data.options))
    this.setData({
      hospitalId: e.detail.optkey
    })
    console.log("select ---" + this.data.hospitalId)
  },
  onScan(e) {
    var that = this;
    console.log(this.data.value);
    wx.scanCode({
      success: function (res) {
        //that.value = res.result;
        let result = res.result.split(",");
        that.addInvoiceData(result);
        console.log(result);
        /**
        * 示例：
        * 调用接口封装方法
        */
     
      },
      fail: function (res) {
        showModal("扫描二维码", "扫描失败，请重试");
      }
    });
  },
  onSearch(e) {
   
    console.log("点击了搜索按钮")
    let list = [0,1,this.data.realInvoice.no,this.data.realInvoice.code," "]
    this.addInvoiceData(list)
  },
  addInvoiceData: function(result){
    wx.showLoading({
      title: '加载中...',
    })
    let invoice = this.data.realInvoice;
    if(result[2]!=null &&result[2].length>0){
       invoice.no = result[2]
    }
    if (result[3] != null && result[3].length > 0) {
       invoice.code = result[3];
    }
    if (result[4] != null && result[4].length > 0) {
      invoice.money = result[4];
    }

    let that = this;
    api.fetchRequest('invoiceCheckAction/queryInvoice.do', invoice)
    .then(function (res) { 
       wx.hideLoading();
        that.setData({
          realInvoice: invoice,
          sysInvoice:{
            no:res.data.rows[0].invoiceNo,
            code: res.data.rows[0].invoiceCode,
            money: res.data.rows[0].NoTaxLineAmt
          }
        })
        console.log(JSON.stringify(res))
   
    }).catch(function (res) {
      wx.hideLoading();
      console.log("发生异常："+res)
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function () {
    var that = this;
    //invoiceCheckAction/orgList.do
    console.log("发票核对页面加载了")
    api.fetchRequest('invoiceCheckAction/orgList.do', {
    }).then(function (res) {
      console.log("医院数据加载")
       
      let options = that.data.options;
      let items = options[0];
      items.options =res.data.rows;
      for (let i = 0; i < items.options.length;i++){
        let name = items.options[i].name;
        items.options[i].text = name;
        }
        //console.log("options --" + JSON.stringify(that.data.options))
        that.setData({
          options:options
        });
     
    }).catch(function (res) {
      console.log("发生异常：" + res)
      wx.showToast({
        title: res.data.msg,
        icon: 'none'
      })
    })
  
    
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})